package com.agentlab.imitation.service;

import java.io.Serializable;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.agentlab.imitation.bean.User;
import com.agentlab.imitation.bean.admin.Admin;
import com.agentlab.imitation.service.admin.AdminService;
import com.agentlab.imitation.service.base.DaoSupport;
import com.agentlab.imitation.util.MD5;

@Service @Transactional
public class UserServiceBean  extends DaoSupport<User> implements UserService {
	@Resource SessionFactory factory;
	@SuppressWarnings("unchecked")
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public User checkUser(String username, String password) {
		//int userid = 0;
		User user = null;
		if("".equals(username) && "".equals(password)){
			return null;
		}
		 List users = factory.getCurrentSession()
				.createQuery(" from User u where u.username=:username and u.password=:userpass")
		.setParameter("username", username).setParameter("userpass", MD5.MD5Encode(password)).list();
		 for (int i = 0; i < users.size(); i++) {
			 user = (User) users.get(i);
			 //userid = users.getUserid();
		 }
		 return user;
	}

//	public void enable(Serializable... usernames) {
//		//1启用状态
//		visible("1",usernames);
//	}
//	
	public boolean exsit(String username) {
		return false;
	}

	public void updatePassword(String userid, String newpassword) {
		Query query = factory.getCurrentSession().createQuery(" update User b set  b.password=:userpass where b.userid=:userid ");
		query.setString("userpass",newpassword );
		query.setString("userid",userid );
	}
//	public void saveUserRid(Admin entity,String[] roleids) {
//		if(entity.getUid() != null){
//			factory.getCurrentSession().update(entity);
//		}else{
//			factory.getCurrentSession().persist(entity);
//		}
//		List<Ur>  urs = queryUr(entity.getUid());
//		for(Ur ur : urs){
//			factory.getCurrentSession().delete(ur);
//		}
//		for(String roleid : roleids){
//			factory.getCurrentSession().save(new Ur(entity.getUid(),roleid));
//		}
//	}
//	@SuppressWarnings("unchecked")
//	public List<Ur> queryUr(int uid) {
//		List<Ur> rslist = factory.getCurrentSession()
//			.createQuery("  from Ur s where s.id.uid =:uid  ").setParameter("uid", uid).list();
//	return rslist ;
//	}
//	
//	@SuppressWarnings("unchecked")
//	public List<Role> queryUr(OrgUsers user ) {
//		List<Role> ustemp = factory.getCurrentSession()
//		.createQuery(" select u.role  from Ur u where u.id.uid=:uid ")
//		.setParameter("uid",user.getUid()).list();
//		return ustemp;
//	}

	public void delete(Serializable... entityids) {
		// TODO Auto-generated method stub
		if(entityids!=null && entityids.length>0){
			StringBuffer jpql = new StringBuffer();
			for(int i=0; i<entityids.length;i++){
				jpql.append("'").append(entityids[i]).append("'").append(",");
			}
			if(jpql.length()>0){
				jpql.deleteCharAt(jpql.length()-1);			
				Query query =factory.getCurrentSession().createQuery(" delete from User u  where u.userid in("+ jpql.toString() +")");
				query.executeUpdate();
			}
		}
		
	}

	public User getUserByEmail(String email) {
		User user = null;
		if(email== null || "".equals(email) ){
			return null;
		}
		 List users = factory.getCurrentSession()
				.createQuery(" from User u where u.email=:email ")
		.setParameter("email", email).list();
		 for (int i = 0; i < users.size(); i++) {
			 user = (User) users.get(i);
			 //userid = users.getUserid();
		 }
		 return user;
	}

	public User getUserByName(String username) {
		User user = null;
		if(username== null || "".equals(username) ){
			return null;
		}
		 List users = factory.getCurrentSession()
				.createQuery(" from User u where u.username=:username ")
		.setParameter("username", username).list();
		 for (int i = 0; i < users.size(); i++) {
			 user = (User) users.get(i);
			 //userid = users.getUserid();
		 }
		 return user;
	}
	
//	private void visible(String statusbz, Serializable ... usernames){
//		if(usernames!=null && usernames.length>0){
//			StringBuffer jpql = new StringBuffer();
//			for(int i=0; i<usernames.length;i++){
//				jpql.append("'").append(usernames[i]).append("'").append(",");
//			}
//			if(jpql.length()>0){
//				jpql.deleteCharAt(jpql.length()-1);			
//				Query query =factory.getCurrentSession().createQuery(" update OrgUsers u set u.status=:visible where u.uid in("+ jpql.toString() +")");
//				query.setString("visible",statusbz);
//				query.executeUpdate();
//			}
//		}
//	}
	
	}
